package me.jiatao.baselibrary.mvp.presenter

import android.content.Context
import com.trello.rxlifecycle3.LifecycleProvider
import me.jiatao.baselibrary.mvp.view.BaseView
import me.jiatao.baselibrary.utils.NetWorkUtil
import javax.inject.Inject

/**
 * MVP架构中Presenter的基类
 */
abstract class BasePresenter<V : BaseView> {
    /**
     * View视图引用
     */
    lateinit var mView: V

    /**
     * Dagger注入，Rx生命周期管理
     */
    @Inject
    lateinit var mLifecycleProvider: LifecycleProvider<*>

    /**
     * 注入的Application对象
     */
    @Inject
    lateinit var context: Context

    /**
     * 检查网络是否可用
     */
    fun checkNetWork(): Boolean {
        if (NetWorkUtil.isNetworkAvailable(context)) {
            return true
        }
        mView.onError("网络不可用")
        return false
    }

    /**
     * 绑定View
     * 初始化资源
     */
    fun attachView(mView: V) {
        this.mView = mView
    }

    /**
     * 解绑View
     * 资源回收
     */
    fun detachView() {

    }
}